#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

class Point
{
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {

    }

    ~Point()
    {

    }
private:
    int _ix;
    int _iy;
};

void test2()
{
    vector<Point> vec;
    Point pt(1, 2);
    vec.push_back(Point(1, 2));
    vec.push_back(pt);
    vec.emplace_back(3, 4);
}

void test()
{
    cout << "sizeof(vector<int>) = " << sizeof(vector<int>) << endl;
    cout << "sizeof(vector<long>) = " << sizeof(vector<long>) << endl;

    cout << endl;
    vector<int> number = {1, 3, 5, 7, 8, 6, 4, 9, 4, 5};
    display(number);
    cout << "number.size() = " << number.size() << endl;
    cout << "number.capacity() = " << number.capacity() << endl;

    cout << endl << "在vector的尾部进行插入与删除" << endl;
    number.push_back(10);
    number.push_back(20);
    display(number);
    cout << "number.size() = " << number.size() << endl;
    cout << "number.capacity() = " << number.capacity() << endl;
    number.pop_back();
    display(number);
    cout << "number.size() = " << number.size() << endl;
    cout << "number.capacity() = " << number.capacity() << endl;

    //vector为什么不支持在头部进行插入与删除？
    //原因是：vector是一端开口的，如果在头部进行插入一个元素，
    //那么后面的元素都要向后移动，从而才可以在头插插入元素，
    //同样的，如果要删除第一个元素，那么后面的元素都要向前
    //移动一个位置，会导致每次会移动所有的元素，时间复杂度O(N)
    
    //获取number的第一个元素的地址？
    &number;//error,不能获取第一个元素的地址
    &number[0];//ok，可以获取第一个元素的地址
    &*number.begin();//ok,可以获取第一个元素的地址
    int *pdate = number.data();

    cout << endl << "在vector的任意位置进行插入" << endl;
    auto it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 300);
    display(number);
    cout << "*it = " << *it << endl;
    cout << "number.size() = " << number.size() << endl;
    cout << "number.capacity() = " << number.capacity() << endl;

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    //迭代器已经失效了，原因是：底层已经发生了扩容
    //解决方案：每次使用迭代器的时候，将迭代器重新置位(更新)
    number.insert(it, 100, 10);
    display(number);
    cout << "*it = " << *it << endl;
    cout << "number.size() = " << number.size() << endl;
    cout << "number.capacity() = " << number.capacity() << endl;

    cout << endl << endl;
    vector<int> vec = {111, 555, 333};
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "*it = " << *it << endl;
    cout << "number.size() = " << number.size() << endl;
    cout << "number.capacity() = " << number.capacity() << endl;

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, {1000, 3000, 5000, 9000});
    display(number);
    cout << "*it = " << *it << endl;
    cout << "number.size() = " << number.size() << endl;
    cout << "number.capacity() = " << number.capacity() << endl;

    cout << endl << "清空vector的元素" << endl;
    number.clear();//删除所有的元素
    number.shrink_to_fit();//回收多余的空间
    cout << "number.size() = " << number.size() << endl;
    cout << "number.capacity() = " << number.capacity() << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

